package com.xgs.web.controller.monitor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xgs.common.utils.sign.Md5Utils;
import com.xgs.common.utils.uuid.IdUtils;
import com.xgs.system.domain.RosterHistory;
import com.xgs.system.domain.SysNotice;
import com.xgs.system.domain.read.BusiTypeStringNumberConverter;
import com.xgs.web.dto.RosterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.xgs.common.annotation.Log;
import com.xgs.common.core.controller.BaseController;
import com.xgs.common.core.domain.AjaxResult;
import com.xgs.common.core.page.TableDataInfo;
import com.xgs.common.enums.BusinessType;
import com.xgs.common.utils.poi.ExcelUtil;
import com.xgs.system.domain.SysOperLog;
import com.xgs.system.service.ISysOperLogService;
import org.springframework.web.client.RestTemplate;

/**
 * 操作日志记录
 * 
 * @author xgs
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController
{
    @Autowired
    private ISysOperLogService operLogService;

    private RestTemplate restTemplate;

    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog)
    {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog)
    {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportEasyExcel(list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable String[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        operLogService.cleanOperLog();
        return success();
    }

    @PostMapping("/push")
    public AjaxResult push(SysOperLog operLog)
    {
        QueryWrapper<SysOperLog> query = Wrappers.query();
        query.in("title", "名单录入数据", "基础版本信息");
        List<SysOperLog> list = operLogService.list(query);
        List<Map<String, String>> jsonArray = new ArrayList<>();
        for (SysOperLog log : list) {
            Map<String, String> map = new HashMap<>();
            map.put("title", log.getTitle());
            if (1 == log.getBusinessType()) {
                map.put("type", "新增");
            } else if (2 == log.getBusinessType()) {
                map.put("type", "修改");
            } else if (3 == log.getBusinessType()) {
                map.put("type", "删除");
            } else if (10 == log.getBusinessType()) {
                map.put("type", "打包");
            } else if (11 == log.getBusinessType()) {
                map.put("type", "下发");
            }
            map.put("oper_name", log.getOperName());
            map.put("oper_time", String.valueOf(log.getOperTime()));
            jsonArray.add(map);
        }

        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json; charset=utf-8");
        headers.set("Accept-Charset", "utf-8");
        headers.add("user", "lane");
        headers.add("pass", Md5Utils.EncryptEx("Xgssoft", true, "xgs", false));
        headers.add("filename", "fileName");

        HttpEntity<String> entity = new HttpEntity<>(jsonArray.toString(), headers);

        restTemplate = new RestTemplate();
        String url = "http://172.16.182.36:8080/" + "2";
        ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);
        JSONObject jsonResult = JSON.parseObject(response.getBody());
        if (jsonResult.getInteger("code") != 200) {
            return AjaxResult.error(-1, jsonResult.getString("msg"));
        }
        return success();
    }
}
